const data = [{
    id: '1',
    name: 'test1',
    children: [
        {
            id: '11',
            name: 'test11',
            children: [
                {
                    id: '111',
                    name: 'test111'
                },
                {
                    id: '112',
                    name: 'test112'
                }
            ]

        },
        {
            id: '12',
            name: 'test12',
            children: [
                {
                    id: '121',
                    name: 'test121'
                },
                {
                    id: '122',
                    name: 'test122'
                }
            ]
        }
    ]
}];

fn("112", data) // 输出 [1， 11， 112]
console.log(fn("112", data))

// 还是一个dfs
function fn(val, data) {
    let result = null
    let temp = []
    function dfs(val, data, temp) {
        data.forEach((item, index) => {
            item.idArr = temp.concat(item.id)
            if (val == item.id) {
                result = item.idArr
            }
            let children = item.children;
            if (children && children.length) {
                dfs(val, children, item.idArr)
            }
        });
    }
    dfs(val, data, temp)
    return result
}